﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheHeGeo.Util;
using TheHeGeoDao;

namespace TheHeGeo.Master
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var str = GetCurrentFile();
                if (Session[SessionConst.SES_USER] == null && 
                    !str.Equals("Login.aspx") && 
                    !str.Equals("Default.aspx")&&
                    !str.Equals("NewAccount.aspx"))
                {
                    if (Session[SessionConst.SES_MENU_LANGUE] == null || Session[SessionConst.SES_MENU_LANGUE].ToString() == "0")
                        Response.Redirect("~/Home/Login.aspx");
                   
                }
            }
            if (Session[SessionConst.SES_MENU_LANGUE] == null || Session[SessionConst.SES_MENU_LANGUE].ToString() == "0")
             GenMenu();
            else
            {
                GenEnglishMenu();
            }
        }

        protected void GenEnglishMenu()
        {
            leftMenu.Controls.Clear();

            string str = GetCurrentDictName();

            switch (str)
            {
                case "Equipments":
                    GenEquipmentsMenuEnglish();
                    break;
                case "User":
                    GenUserMenuEnglish();
                    break;
                case "Admin":
                    GenAdminMenuEnglish();
                    break;
                case "Tools":
                    GenToolMenu();
                    break;
                default:
                    GenDefaultMenuEnglish();
                    break;

            }

        }

        protected void GenToolMenu()
        {
            GenHomeMenu();
            var thiNghiem = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thiNghiem.MenuTxt = "Phòng thí nghiệm";
            thiNghiem.Url = "../Tools/Laboratory.aspx";
            leftMenu.Controls.Add(thiNghiem);

            var tnNenKctt = (UcMenu) Page.LoadControl("~/Master/UcMenu.ascx");
            tnNenKctt.MenuTxt = "T.n Nén kết cấu  thông tin";
            tnNenKctt.Url = "../Tools/ThiNghiemNenKetCauThongTin.aspx?t=1";
            leftMenu.Controls.Add(tnNenKctt);
        }

        protected void GenUserMenuEnglish()
        {
            GenHomeMenuEnglish();

            var nsd = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            nsd.MenuTxt = "Setup";
            nsd.Url = "../User/UserDefault_English.aspx";
            leftMenu.Controls.Add(nsd);

            var da = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            da.MenuTxt = "Project";
            da.Url = "../User/ProjManagement_English.aspx";
            leftMenu.Controls.Add(da);

            var eqCf = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqCf.MenuTxt = "Equipment";
            eqCf.Url = "../User/EquipmentManager_English.aspx";
            leftMenu.Controls.Add(eqCf);

            var eqma = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqma.MenuTxt = "Sensor";
            eqma.Url = "../User/SensorManagement_English.aspx";
            leftMenu.Controls.Add(eqma);

            var gtcb = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            gtcb.MenuTxt = "Warning management";
            gtcb.Url = "../User/WarningManagerment_English.aspx";
            leftMenu.Controls.Add(gtcb);
            //var usw = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            //usw.MenuTxt = "List User Warning management";
            //usw.Url = "../User/UserWarningManagerment_English.aspx";
            //leftMenu.Controls.Add(usw);
        }

        protected void GenEquipmentsMenuEnglish()
        {
            GenHomeMenuEnglish();

            var thietbi = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thietbi.MenuTxt = "Monitoring management";
            thietbi.Url = "../Equipments/EquipmentDefault_English.aspx"; leftMenu.Controls.Add(thietbi);

            var eqChart = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqChart.MenuTxt = "Show chart";
            eqChart.Url = "../Equipments/EquipmentMornitoring_English.aspx";
            leftMenu.Controls.Add(eqChart);

            var eqData = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqData.MenuTxt = "Show data";
            eqData.Url = "../Equipments/EquipmentData_English.aspx";
            leftMenu.Controls.Add(eqData);

            var eqSensor = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqSensor.MenuTxt = "Sensor controller";
            eqSensor.Url = "../Equipments/SensorMornitoring_English.aspx";
            leftMenu.Controls.Add(eqSensor);
        }

        protected void GenHomeMenuEnglish()
        {
            var main = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            main.MenuTxt = "Home";
            main.Url = "../Home/Default_English.aspx";
            leftMenu.Controls.Add(main);
        }

        protected void GenDefaultMenuEnglish()
        {
            GenHomeMenuEnglish();

            var nsd = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            nsd.MenuTxt = "Setup";
            nsd.Url = "../User/UserDefault_English.aspx";
            leftMenu.Controls.Add(nsd);

            var thietbi = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thietbi.MenuTxt = "Monitoring management";
            thietbi.Url = "../Equipments/EquipmentDefault_English.aspx";
            leftMenu.Controls.Add(thietbi);

            var thiNghiem = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thietbi.MenuTxt = "Laboratory";
            thietbi.Url = "../Tools/Laboratory.aspx";
            leftMenu.Controls.Add(thiNghiem);

            if (Session[SessionConst.SES_USER] != null && ((SessionUser)Session[SessionConst.SES_USER]).NguoiSuDung.Role <= 1)
            {
                var admin = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
                admin.MenuTxt = "Admin";
                admin.Url = "../Admin/AdminSensorManager.aspx";
                leftMenu.Controls.Add(admin);
            }
        }

        protected void GenAdminMenuEnglish()
        {
            GenHomeMenu();
            var dtCorrect = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            dtCorrect.MenuTxt = "Type sensor management";
            dtCorrect.Url = "../Admin/AdminSensorManager.aspx";
            leftMenu.Controls.Add(dtCorrect);
        }

        protected void GenMenu()
        {
            leftMenu.Controls.Clear();

            string str = GetCurrentDictName();

            switch (str)
            {
                case "Equipments":
                    GenEquipmentsMenu();
                        break;
                case "User":
                    GenUserMenu();
                    break;
                case "Admin":
                    GenAdminMenu();
                    break;
                case "Tools":
                    GenToolMenu();
                    break;
                default:
                    GenDefaultMenu();
                    break;
                
            }
            
        }

        protected void GenUserMenu()
        {
            GenHomeMenu();

            var nsd = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            nsd.MenuTxt = "Cài đặt";
            nsd.Url = "../User/UserDefault.aspx";
            leftMenu.Controls.Add(nsd);

            var da = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            da.MenuTxt = "Dự án";
            da.Url = "../User/ProjManagement.aspx";
            leftMenu.Controls.Add(da);

            var eqCf = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqCf.MenuTxt = "Thiết bị";
            eqCf.Url = "../User/EquipmentManager.aspx";
            leftMenu.Controls.Add(eqCf);

            var eqma= (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqma.MenuTxt = "Sensor";
            eqma.Url = "../User/SensorManagement.aspx";
            leftMenu.Controls.Add(eqma);

            var gtcb = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            gtcb.MenuTxt = "Quản lý cảnh báo";
            gtcb.Url = "../User/WarningManagerment.aspx";
            leftMenu.Controls.Add(gtcb);
            //var usw = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            //usw.MenuTxt = "Quản lý Ds User nhận cảnh báo";
            //usw.Url = "../User/UserWarningManagerment.aspx";
            //leftMenu.Controls.Add(usw);
        }

        protected void GenEquipmentsMenu()
        {
            GenHomeMenu();

            var thietbi = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thietbi.MenuTxt = "Quản lý quan trắc";
            thietbi.Url = "../Equipments/EquipmentDefault.aspx";
            leftMenu.Controls.Add(thietbi);

            var eqChart = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqChart.MenuTxt = "Biểu đồ";
            eqChart.Url = "../Equipments/EquipmentMornitoring.aspx";
            leftMenu.Controls.Add(eqChart);

            var eqData = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqData.MenuTxt = "Dữ liệu";
            eqData.Url = "../Equipments/EquipmentData.aspx";
            leftMenu.Controls.Add(eqData);

            var eqSensor = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            eqSensor.MenuTxt = "Điều khiển Sensor";
            eqSensor.Url = "../Equipments/SensorMornitoring.aspx";
            leftMenu.Controls.Add(eqSensor);
        }

        protected void GenHomeMenu()
        {
            var main = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            main.MenuTxt = "Trang chủ";
            main.Url = "../Home/Default.aspx";
            leftMenu.Controls.Add(main);
        }

        protected void GenDefaultMenu()
        {
            GenHomeMenu();

            var nsd = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            nsd.MenuTxt = "Cài đặt";
            nsd.Url = "../User/UserDefault.aspx";
            leftMenu.Controls.Add(nsd);

            var thietbi = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thietbi.MenuTxt = "Quản lý quan trắc";
            thietbi.Url = "../Equipments/EquipmentDefault.aspx";
            leftMenu.Controls.Add(thietbi);

            var thiNghiem = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            thiNghiem.MenuTxt = "Phòng thí nghiệm";
            thiNghiem.Url = "../Tools/Laboratory.aspx";
            leftMenu.Controls.Add(thiNghiem);

            if (Session[SessionConst.SES_USER]!=null&&((SessionUser)Session[SessionConst.SES_USER]).NguoiSuDung.Role <= 1)
            {
                var admin = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
                admin.MenuTxt = "Admin";
                admin.Url = "../Admin/AdminSensorManager.aspx";
                leftMenu.Controls.Add(admin);
            }
        }

        protected void GenAdminMenu()
        {
            GenHomeMenu();
            var dtCorrect = (UcMenu)Page.LoadControl("~/Master/UcMenu.ascx");
            dtCorrect.MenuTxt = "Quản lý loại sensor";
            dtCorrect.Url = "../Admin/AdminSensorManager.aspx";
            leftMenu.Controls.Add(dtCorrect);
        }

        public static string GetCurrentDictName()
        {
            string sPath = HttpContext.Current.Request.Url.AbsolutePath;
            var oInfo = new System.IO.FileInfo(sPath);
            var str = oInfo.Directory == null ? string.Empty : oInfo.Directory.Name;
            return str;
        }

        public static string GetCurrentFile()
        {
            string sPath = HttpContext.Current.Request.Url.AbsolutePath;
            var oInfo = new System.IO.FileInfo(sPath);
            return oInfo.Name;
        }

    }
}